package DAO;

import Entity.Manager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DAO_Manager {

    Connection conn = null;
    Statement state = null;
    ResultSet rs = null;

    public DAO_Manager() {
        //connection1("jdbc:odbc:StudentManagementDatabase");
        connection4("jdbc:sqlserver://localhost"
                + ":1433;databaseName=StudentManagementDatabase","sa","123456");    
    }

    public final void connection1(String url) {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            try {
                conn = DriverManager.getConnection(url);
            } catch (SQLException ex) {
                Logger.getLogger(DAO_Student.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("DAO_Manager is connected");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DAO_Student.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public final void connection4(String url, String username, String password){
         try {
            //create driver
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            
            //get connection
            conn=DriverManager.getConnection(url, 
                    username, password);
            System.out.println("DAO Manager is connected");
            
        } catch (ClassNotFoundException | SQLException ex) {
        }
    }     
    public void closeConnection() {
        try {
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Student.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * Author: HieuTD
     * @param sql
     * @return 
     */
    public ResultSet getData(String sql) {
        ResultSet dataset = null;
        try {
            state = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            dataset = state.executeQuery(sql);
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Manager.class.getName()).log(Level.SEVERE, null, ex);
        }

        return dataset;

    }
    
    /**
     * Author: HieuTD
     * @param person
     * @return 
     */
    public boolean checkLogin(Manager person) {
        
        try {
            String sql = "SELECT COUNT(id) FROM Manager WHERE username='" + person.getUsername() + "' AND password='" + person.getPassword() + "'";
            Statement statement = conn.createStatement();
            ResultSet data = statement.executeQuery(sql);
            if(data.next()) {
                if(data.getInt(1) == 1) return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Manager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    
    public int getUid(String name) {
        int n = 0;
        try {
            String sql = "SELECT id FROM Manager WHERE username='" + name +"'";
            Statement statement = conn.createStatement();
            ResultSet data = statement.executeQuery(sql);
            while (data.next()) {
                n = data.getInt(1);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Manager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return n;
    }
    
    
    /**
     * Just for test connection. Shift+F6 to run me!
     * @param args
     */
    public static void main(String[] args) {
        DAO_Manager s = new DAO_Manager();
    }    
}
